import os.path
from lxml import etree
import time
import datetime
import diytool
from diytool import randomStr
from diytool import uuid_maker
from send_soap_xml import post_C2
import warnings
import logging

def write_xml(path):

    parser = etree.XMLParser(encoding = "utf-8")
    #tree= etree.parse(r"/home/projects/xmlsource/program_add_copyright.xml",parser=parser)
    tree= etree.parse(r"/home/projects/xmlsource/program_add.xml",parser=parser)

    code = 'program' + uuid_maker(25)
    #mcode = 'movie'+ uuid_maker(27)
    #pcode = 'picture' + uuid_maker(25)

    #print(etree.tostring(tree,encoding = 'utf-8').decode('utf-8'))
    programObjNode = tree.xpath('//Object[@ElementType="Program"]')[0]
    
    programObjNode.set("Code",code)
    programObjNode.set("ID",code)
    #生成8位随机字符串
    salt = randomStr(10)
    pname = tree.xpath('//Object[@ElementType="Program"]/Property[@Name="Name"]')[0]
    salt = "爱上" + diytool.programNamelist[int(a)]+"_"+salt
    pname.text = salt
    #替换导演
    director = diytool.directorlist[int(a)]
    directNode = tree.xpath('//Object[@ElementType="Program"]/Property[@Name="Director"]')[0]
    directNode.text = director
    # 替换演员
    actor = diytool.actorlist[int(a)]
    actorNode = tree.xpath('//Object[@ElementType="Program"]/Property[@Name="Kpeople"]')[0]
    actorNode.text = actor
    #替换节目形态
    #pgmcategory = diytool.programcategorylist[int(a)].split(',')[0]
    #pgmcategoryNode = tree.xpath('//Object[@ElementType="Program"]/Property[@Name="PgmCategory"]')[0]
    #pgmcategoryNode.text = pgmcategory
    # 替换二级分类
    #sencondclass = diytool.programcategorylist[int(a)].split(',')[1]
    #sencondclassNode = tree.xpath('//Object[@ElementType="Program"]/Property[@Name="PgmSndClass"]')[0]
    #sencondclassNode.text = sencondclass
    # 替换描述信息
    description = programObjNode.find('Property[@Name="Description"]')
    description.text = salt
    
    
    movieObjNodes = tree.xpath('//Object[@ElementType="Movie"]')
    for i in range(len(movieObjNodes)):
        movieObjNode = tree.xpath('//Object[@ElementType="Movie"]')[i]
        mcode = 'movie' + uuid_maker(27)
        movieObjNode.set("Code",mcode)
        movieObjNode.set("ID",mcode)
        moviename = movieObjNode.find('Property[@Name="Name"]')
        moviename.text = salt+str(i);
        moviemapping = tree.xpath('//Mapping[@ElementType="Movie"][@ParentType="Program"]')[i]
        moviemapping.set("ParentCode", code)
        moviemapping.set("ParentID", code)
        moviemapping.set("ElementCode", mcode)
        moviemapping.set("ElementID", mcode)

    picObjNodes = tree.xpath('//Object[@ElementType="Picture"]')
    for k in range(len(picObjNodes)):
        picObjNode = tree.xpath('//Object[@ElementType="Picture"]')[k]
        pcode = 'picture' + uuid_maker(25)
        picObjNode.set("Code",pcode)
        picObjNode.set("ID",pcode)
        picturemapping = tree.xpath('//Mapping[@ElementType="Program"][@ParentType="Picture"]')[k]
        picturemapping.set("ParentCode", pcode)
        picturemapping.set("ParentID", pcode)
        picturemapping.set("ElementCode", code)
        picturemapping.set("ElementID", code)

    tree.write(path, pretty_print=True, xml_declaration=True, encoding='utf-8')

    logging.info("generate program :"+ salt)

diytool.writelog('logs/program.log')
a =200
count=0
while True:
 
    #request_url = "http://172.19.64.71:8138/scms-injection-adapter/axis/services/ctms"
    #request_url = "http://10.48.115.55:6080/axis/services/ctms"
    request_url = "http://172.19.64.72:8138/scms-injection-adapter/axis/services/ctms"
    CSPID = "10089"
    LSPID = "10089"
    CorrelateID = diytool.timeStr()
    nowdate = datetime.datetime.now().strftime('%Y%m%d')
    hour = datetime.datetime.now().strftime('%H')
    #path ="D:/opt/wacos/worksheet/"
    path = "/opt/wacos/worksheet/program/" + nowdate+ "/" + hour + "/"
    xmlfile  = path + "program" + uuid_maker(25)+".xml"
    if not os.path.exists(path):
        print("# path not exists")
        os.makedirs(path)
    write_xml(xmlfile)
   # FileURL = path.replace("D:", "ftp://wacos:wacos@172.20.1.20:21") + xmlfilename
    warnings.filterwarnings("ignore")
    FileURL ="ftp://wacos:wacos@172.19.97.208:21/" + xmlfile
    logging.info("program_worksheet_xml ftp file path-->>" + FileURL)
    post_C2(request_url,CSPID,LSPID,CorrelateID,FileURL)
    a = a + 1
    count = count+1
    logging.info("send program count: "+ str(count))
    sleeptime = 1
    logging.info("waiting for "+ str(sleeptime)+ "s ......")
    time.sleep(sleeptime)


